/**
 * @单位名称：飞羽个人 Copyright (c) 2017 All Rights Reserved.
 * @系统名称：测试
 * @工程名称：
 * @文件名称:
 * @类路径:
 */
package com.feiyu.controller;

import com.feiyu.bean.HouseProject;
import com.feiyu.common.controller.BaseController;
import com.feiyu.common.util.CustomExcelUtil;
import com.feiyu.service.HouseProjectService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 *
 * @see
 * @author nanshouxiao
 * @date 2018-5-16 9:20:13
 * @version V1.0.0
 */
@Controller
@RequestMapping("houseProject")
public class HouseProjectController extends BaseController<HouseProject> {

    @Autowired
    private HouseProjectService houseProjectService;

    private static Pattern batchNumberPattern = Pattern.compile("总第(.*?)批");

    private static final String[] titles = {""};

    @PostMapping("importHouseProject")
    @ResponseBody
    public List<HouseProject> importHouseProject(MultipartFile file, String batchNumber) {
        List<HouseProject> houseProjectList = CustomExcelUtil.handleProjectExcel(file);
        houseProjectList.forEach(houseProject -> houseProject.setBatchNumber(batchNumber));
        houseProjectService.batchSaveEntity(houseProjectList);
        return houseProjectList;

    }

    @GetMapping("getHouseProjectList")
    @ResponseBody
    public List<HouseProject> getHouseProjectList(HouseProject houseProject) {
        List<HouseProject> houseProjectList = houseProjectService.findByParamObj(houseProject, new RowBounds());
        return houseProjectList;

    }

    @PostMapping("importAllInfoByFilepath")
    @ResponseBody
    public String importAllInfoByFilepath(String filePath) {
        String batchNumber = this.getBatchNumber(filePath);
        houseProjectService.importAllInfoByFilepath(filePath, batchNumber);
        return "第" + batchNumber + " 批数据导入成功";

    }

    private String getBatchNumber(String fileName) {
        String result = null;
        Matcher matcher = batchNumberPattern.matcher(fileName);
        if (matcher.find()) {
            result = matcher.group(1);
        }
        return result;
    }
}